package pt.isel.pdm.ouryamba;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

public class PreferencesActivity extends PreferenceActivity implements OnPreferenceChangeListener
{
	
	private static final String PERIOD_PREF = "automaticUpdatePeriodPref";
	private static final String UPDATE_PREF = "automaticUpdatePref";
	private Preference updatePeriod;
	
	
//PreferenceActivity methods
	
	/**
	 * Initialize some global references and checks whether should hide some items or not.
	 * 
	 * If the automatic update item is checked means another preference item should be 
	 * displayed also (update period).
	 * 
	 * Set this class has a listener for the automatic update check box so any changes could
	 * hide or display the extra preference mentioned above.
	 * 
	 * @see #setAvailabilityOnPreferences(boolean)
	 * */
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		CheckBoxPreference cbp = (CheckBoxPreference)findPreference(UPDATE_PREF);
		cbp.setOnPreferenceChangeListener(this);
		updatePeriod = findPreference(PERIOD_PREF);
		setAvailabilityOnPreferences(cbp.isChecked());
	}

	
//OnPreferenceChangeListener methods
	/**
	 * Whenever the check box item changes state, other preference needed to change state also.
	 * 
	 * @see #setAvailabilityOnPreferences(boolean)
	 * */
	@Override
	public boolean onPreferenceChange(Preference pref, Object obj) 
	{
		if(pref.getKey().equals(UPDATE_PREF))
		{
			setAvailabilityOnPreferences((Boolean)obj);
		}
		return true;
	}

	
//Own methods
	/**
	 * Enables or disables the update period preference item.
	 * 
	 * @param enable Indicates whether to hide (false) or show(true) the above mentioned preference.
	 * */
	private void setAvailabilityOnPreferences(boolean enable)
	{
		updatePeriod.setEnabled(enable);
	}

}
